package gwt.immunology.server.converter;

import static gwt.immunology.shared.GenericDtoKeys.ID;
import static gwt.immunology.shared.GenericDtoKeys.BIRTHDAY;
import static gwt.immunology.shared.GenericDtoKeys.CITY;
import static gwt.immunology.shared.GenericDtoKeys.COUNTRY;
import static gwt.immunology.shared.GenericDtoKeys.CREATION_DATE;
import static gwt.immunology.shared.GenericDtoKeys.DISTRICT;
import static gwt.immunology.shared.GenericDtoKeys.FIRST_NAME;
import static gwt.immunology.shared.GenericDtoKeys.FLAT;
import static gwt.immunology.shared.GenericDtoKeys.GENDER;
import static gwt.immunology.shared.GenericDtoKeys.HOUSE;
import static gwt.immunology.shared.GenericDtoKeys.LAST_NAME;
import static gwt.immunology.shared.GenericDtoKeys.MEDICINE_CARD_ID;
import static gwt.immunology.shared.GenericDtoKeys.MIDDLE_NAME;
import static gwt.immunology.shared.GenericDtoKeys.OTHER_INFO;
import static gwt.immunology.shared.GenericDtoKeys.REGION;
import static gwt.immunology.shared.GenericDtoKeys.STREET;
import gwt.immunology.shared.GenericDto;
import gwt.immunology.shared.ResultSetDto;
import gwt.immunology.shared.enums.GenderEnum;

import java.util.List;

import models.MedicineCard;
import models.Patient;

public class PatientDtoConverter {

    public static GenericDto convertPatientToGenericDto(Patient patient) {
	GenericDto result = new GenericDto();
	if (patient != null) {
	    result.put(ID, patient.patientId);
	    result.put(FIRST_NAME, patient.firstName);
	    result.put(MIDDLE_NAME, patient.middleName);
	    result.put(LAST_NAME, patient.lastName);
	    result.put(GENDER, patient.gender.toString());
	    result.put(BIRTHDAY, patient.birthday);
	    result.put(COUNTRY, patient.country);
	    result.put(REGION, patient.region);
	    result.put(DISTRICT, patient.district);
	    result.put(CITY, patient.city);
	    result.put(STREET, patient.street);
	    result.put(HOUSE, patient.house);
	    result.put(FLAT, patient.flat != null ? patient.flat : null);
	    result.put(MEDICINE_CARD_ID, patient.medicineCard.medicineCardId);
	    result.put(CREATION_DATE, patient.medicineCard.creationDate);
	    result.put(OTHER_INFO, patient.medicineCard.otherInfo);
	}
	return result;
    }

    public static Patient convertGenericDtoToPatient(GenericDto patientDto) {
	Patient result = null;
	if (patientDto != null && !patientDto.isEmpty()) {
	    result = new Patient();
	    result.patientId = patientDto.getLong(ID);
	    result.firstName = patientDto.getString(FIRST_NAME);
	    result.middleName = patientDto.getString(MIDDLE_NAME);
	    result.lastName = patientDto.getString(LAST_NAME);
	    String gender = patientDto.getString(GENDER);
	    if (gender != null) {
		result.gender = GenderEnum.valueOf(gender);
	    } else {
		result.gender = null;
	    }
	    result.birthday = patientDto.getDate(BIRTHDAY);
	    result.country = patientDto.getString(COUNTRY);
	    result.region = patientDto.getString(REGION);
	    result.district = patientDto.getString(DISTRICT);
	    result.city = patientDto.getString(CITY);
	    result.street = patientDto.getString(STREET);
	    result.house = patientDto.getString(HOUSE);
	    result.flat = patientDto.getInt(FLAT);
	    result.medicineCard = new MedicineCard();
	    result.medicineCard.medicineCardId = patientDto.getLong(MEDICINE_CARD_ID);
	    result.medicineCard.creationDate = patientDto.getDate(CREATION_DATE);
	    result.medicineCard.otherInfo = patientDto.getString(OTHER_INFO);
	}
	return result;
    }

    public static ResultSetDto convertPatientsToResultSetDto(List<Patient> patients) {
	ResultSetDto result = new ResultSetDto();
	if (patients != null && !patients.isEmpty()) {
	    for (Patient patient : patients) {
		GenericDto patientDto = convertPatientToGenericDto(patient);
		result.add(patientDto);
	    }
	}
	return result;
    }

}
